<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<!--edge浏览器H5兼容设置-->
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<!--360浏览器H5兼容设置-->
		<meta name="renderer" content="webkit" />
		<title>助农商城</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!--导入核心文件-->
		<script src="../bootstrap3/js/holder.js"></script>
		<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
		<script src="../bootstrap3/jquery-1.9.1.min.js"></script>
		<script src="../bootstrap3/js/bootstrap.js"></script>
		<!-- 字体图标 -->
		<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
		<link rel="stylesheet" type="text/css" href="../css/layout.css" />
		<link rel="stylesheet" type="text/css" href="../css/top.css" />
		<link rel="stylesheet" type="text/css" href="../css/footer.css" />
		<link rel="stylesheet" type="text/css" href="../css/cart.css" />
<!--		<script src="../js/cart.js" type="text/javascript" charset="utf-8"></script>-->
	</head>

	<body>
		<!--头部-->
		<header class="header">
			<!--电脑商城logo-->
			<div class="row">
				<div class="col-md-3">
					<a href="index.html">
						<img src="../images/index/stumalllogo.jpg" />
					</a>
				</div>
				<!--快捷选项-->
				<div class="col-md-9 top-item">
					<ul class="list-inline pull-right">
						<li><a href="/toCart"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
						<li class="li-split">|</li>
						<li>
							<!--下列列表按钮 ：管理-->
							<div class="btn-group">
								<button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">
									<span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span class="caret"></span></span>
								</button>
								<ul class="dropdown-menu top-dropdown-ul" role="menu">
									<li><a href="/toUpdate">修改密码</a></li>
									<li><a href="/toAddress">收货管理</a></li>
								</ul>
							</div>
						</li>
						<li class="li-split">|</li>
						<li><a href="/web/login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
					</ul>
				</div>
			</div>
		</header>
		<!--导航 -->
		<!--分割导航和顶部-->
		<div class="row top-nav">
			<div class="col-md-6">
				<ul class="nav nav-pills">
					<li>
						<a href="#"></a>
					</li>
					<li class="active"><a href="/"><span class="fa fa-home"></span></a></li>
				</ul>
			</div>
			<div class="col-md-6">
				<form action="search.html" class="form-inline pull-right" role="form">
					<div class="form-group">
						<input type="text" class="form-control" id="search" name="search" placeholder="请输入商品名称进行搜索">
					</div>
					<button type="submit" class="btn btn-default btn-sm"><span class="fa fa-search"></span></button>
				</form>
			</div>
		</div>
		<!--头部结束-->
		<!--导航结束-->
		<div class="container">
			<div class="col-md-offset-1 col-md-10">
				<div class="panel panel-primary">
					<div class="panel-heading">
						<p class="panel-title"><span class="fa fa-shopping-cart"></span> 购物车</p>
					</div>
					<div class="panel-body">
						<form action="/toOrder" role="form">
							<!--购物车表格开始-->
							<table class="cart-table" width="100%">
								<thead>
									<tr>
										<th width="110"></th>
										<th width="29%">商品</th>
										<th width="11%">单价</th>
										<th width="15%">数量</th>
										<th width="11%">金额</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody id="cart-list" class="cart-body">
								</tbody>
							</table>
							<div class="total-bar">
								<span class="pull-right">已选商品
								<span id="selectCount">0</span>件 总价¥
								<span id="selectTotal">0</span>元
								</span>
							</div>
							<div>
								<span class="pull-right">
								<input type="submit" value="  结  算  " class="btn btn-primary btn-lg" />
							</span>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			// $(function() {
			// 	//返回链接
			// 	$(".link-account").click(function() {
			// 		location.href = "orderConfirm.html";
			// 	})
			// })
			$(document).ready(function () {
				$("#cart-list").empty();
				$.ajax({
					url:"/cart/getCart",
					type:"get",
					dataType:"JSON",
					success:function (json) {
						if(json.state == 200){
							let list = json.data;
							let count = 0;
							let total = 0;
							for (let i = 0; i < list.length; i++) {
								count++;
								total += list[i].singlePrice*list[i].num;
								let tr = '<tr>\n' +
										'<td><img src="..#{image}.jpg" class="img-responsive" /></td>\n' +
										'<td>#{title}</td>\n' +
										'<td>¥<span id="goodsPrice#{cid1}">#{singlePrice}</span></td>\n' +
										'<td>\n' +
										'<input id="price-#{cid2}" type="button" value="-" class="num-btn" onclick="reduceNum(#{cid7})" />\n' +
										'<input id="goodsCount#{cid3}" type="text" size="2" readonly="readonly" class="num-text" value="#{num}">\n' +
										'<input id="price+#{cid4}" class="num-btn" type="button" value="+" onclick="addNum(#{cid6})" />\n' +
										'</td>\n' +
										'<td><span id="goodsCast#{cid5}">#{totalPrice}</span></td>\n' +
										'<td>\n' +
										'<input type="button" onclick="delCart(#{cid8})" class="cart-del btn btn-default btn-xs" value="删除" />\n' +
										'</td>\n' +
										'</tr>';
								tr = tr.replace(/#{cid1}/g,list[i].cid);
								tr = tr.replace(/#{cid2}/g,list[i].cid);
								tr = tr.replace(/#{cid3}/g,list[i].cid);
								tr = tr.replace(/#{cid4}/g,list[i].cid);
								tr = tr.replace(/#{cid5}/g,list[i].cid);
								tr = tr.replace(/#{cid6}/g,list[i].cid);
								tr = tr.replace(/#{cid7}/g,list[i].cid);
								tr = tr.replace(/#{cid8}/g,list[i].cid);
								tr = tr.replace(/#{image}/g,list[i].image);
								tr = tr.replace(/#{singlePrice}/g,list[i].singlePrice);
								tr = tr.replace(/#{title}/g,list[i].title);
								tr = tr.replace(/#{num}/g,list[i].num);
								tr = tr.replace(/#{totalPrice}/g,list[i].singlePrice*list[i].num);

								$("#cart-list").append(tr);
							}
							$("#selectCount").text(count);
							$("#selectTotal").text(total);
						}
					},
					error:function (xhr) {
						alert("未知错误"+xhr.status);
					}
				});
			});

			function addNum(cid) {
				let num = $("#goodsCount" + cid).val();
				num = Number(num)+1;
				$.ajax({
					url:"/cart/updateCart",
					type:"post",
					data:{
						cid:cid,
						num:num
					},
					dataType:"JSON",
					success:function (json) {
						if(json.state == 200){
							$("#goodsCount"+cid).val(num);
							let singlePrice = $("#goodsPrice"+cid).html();
							let totalPrice = singlePrice*(num);
							$("#goodsCast"+cid).html(totalPrice);
							let total = $("#selectTotal").text();
							$("#selectTotal").text(total-(singlePrice*(num-1))+(singlePrice*(num)));
						}else {
							alert("添加失败："+json.message)
						}
					},
					error:function (xhr) {
						alert("未知错误"+xhr.status);
					}
				});
			}

			function reduceNum(cid) {
				let num = $("#goodsCount" + cid).val();
				num = Number(num)-1;
				$.ajax({
					url:"/cart/updateCart",
					type:"post",
					data: {
						cid:cid,
						num:num
					},
					dataType:"JSON",
					success:function (json) {
						if(json.state == 200){
							$("#goodsCount"+cid).val(num);
							let singlePrice = $("#goodsPrice"+cid).html();
							let totalPrice = singlePrice*(num);
							$("#goodsCast"+cid).html(totalPrice);
							let total = $("#selectTotal").text();
							$("#selectTotal").text(total-(singlePrice*(num+1))+(singlePrice*(num)));
						}else {
							alert("添加失败："+json.message)
						}
					},
					error:function (xhr) {
						alert("未知错误"+xhr.status);
					}
				});
			}

			function delCart(cid) {
				let num = $("#goodsCount" + cid).val();
				num = Number(num)-1;
				$.ajax({
					url:"/cart/deleteCart",
					type:"post",
					data: {
						cid:cid
					},
					dataType:"JSON",
					success:function (json) {
						if(json.state == 200){
							location.href = "/toCart";
						}else {
							alert("删除失败："+json.message)
						}
					},
					error:function (xhr) {
						alert("未知错误"+xhr.status);
					}
				});
			}
		</script>
	</body>

</html>